<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex">
          <h1 class="f16">平板采集用户设定</h1>
          <JDialog
            :tableHeader="tableHeader"
            :width="73"
            @removeOne="removeOne"
            :currentTableRow="currentTableRow"
            @confirm="confirm"
            :unEdit="false"
          />
        </div>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" @currentRow="currentRow" />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex">
          <h1 class="f16">采集部门设定</h1>
          <JDialog
            :tableHeader="tableHeader2"
            :width="73"
            @removeOne="removeOne2"
            :currentTableRow="currentTableRow2"
            @confirm="confirm2"
            :unEdit="false"
          />
        </div>
        <Table :tableHeader="tableHeader2" :tableData="tableData2" class="mt10" :isLoading="isLoading2" @currentRow="currentRow2" />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { formateData } from '@/plugins/mixinJ'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'TheTabletCapturesUserSettings'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入用户名',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入采集类型',
    inputValue: ''
  }
])
const tableHeader = ref([
  {
    label: '用户名',
    prop: 'collectUserName'
  },
  {
    label: '密码',
    prop: 'collectUserPwd'
  },
  {
    label: '采集类型',
    type: 'selectJ',
    prop: 'collectType',
    options: [
      {
        label: '生产',
        value: '生产'
      },
      {
        label: '品质',
        value: '品质'
      },
      {
        label: '投产',
        value: '投产'
      },
      {
        label: '产出',
        value: '产出'
      },
      {
        label: '全检',
        value: '全检'
      }
    ]
  },
  {
    label: '采集部门类型',
    type: 'selectJ',
    prop: 'deptType',
    options: [
      {
        key: '成型',
        label: '成型',
        value: '成型'
      },
      {
        key: '针车',
        label: '针车',
        value: '针车'
      }
    ]
  }
])
// 表一模块
const tableData = ref([]) as any
const imgList = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcCollectUser/getCollectUser', {
    collectUserName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入用户名'
    )?.inputValue,
    collectType: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入采集类型'
    )?.inputValue
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any) => {
    return item
  })
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  getTableList2()
}
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

const tableHeader2 = ref([
  {
    label: '部门代码',
    prop: 'deptNo'
  },
  {
    label: '部门名称',
    prop: 'deptName'
  }
])
const tableData2 = ref([])
const isLoading2 = ref(true)
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.get<any, any>(`/pcCollectUser/getDeptByCollectUser?id=${currentTableRow.value.id}`)
  isLoading2.value = false
  tableData2.value = tableDataList.list
  total2.value = tableDataList.total
  currentRow2()
}, 500)
const currentTableRow2 = ref({}) as any
const currentRow2 = async (row: any = tableData.value[0]) => {
  currentTableRow2.value = row
}

// Dialog确认添加/修改按钮
const confirm = async (obj: any, dialogVisible: any, title: any) => {
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await LoginWork.post(`/pcCollectUser/addCollectUser`, {
      ...obj,
      collectType: obj.collectType.value,
      deptType: obj.deptType.value,
      sysDate: null
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  } else if (title === '修改') {
    const res = (await LoginWork.post(`/pcCollectUser/updateCollectUser`, {
      ...obj,
      collectType: obj.collectType.value ? obj.collectType.value : obj.collectType,
      deptType: obj.deptType.value ? obj.deptType.value : obj.deptType,
      sysDate: null
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  }
  // 关闭弹出框
  dialogVisible.value = false
  // 重新获取表格数据
  getTableList()
}
// 删除
const removeOne = async () => {
  // 向服务器发送请求，更新外派单信息
  const res = (await LoginWork.get(`/pcCollectUser/deleteCollectUser?id=${currentTableRow.value.id}`)) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.message)
  } else {
    JMessage.error(res.message)
  }
  // 重新获取表格数据
  getTableList()
}

// Dialog确认添加/修改按钮
const confirm2 = async (obj: any, dialogVisible: any, title: any) => {
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await LoginWork.post(`/pcCollectUser/addCollectDept`, {
      ...obj,
      collectUserId: currentTableRow.value.id
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  } else if (title === '修改') {
    const res = (await LoginWork.post(`/pcCollectUser/updateCollectDept`, {
      ...obj,
      deptName2: obj.deptName,
      collectUserId: currentTableRow.value.id,
      deptNo: currentTableRow2.value.deptNo,
      deptNo2: obj.deptNo
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  }
  // 关闭弹出框
  dialogVisible.value = false
  // 重新获取表格数据
  getTableList2()
}
// 删除
const removeOne2 = async () => {
  // 向服务器发送请求，更新外派单信息
  const res = (await LoginWork.post(`/pcCollectUser/deleteCollectDept`, currentTableRow2.value)) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.message)
  } else {
    JMessage.error(res.message)
  }
  // 重新获取表格数据
  getTableList2()
}
</script>

<style lang="less" scoped></style>
